package com.example.cs342messenger;

import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MessageListViewAdapter extends ArrayAdapter<Comment> {
	
	private LinearLayout wrapper;
	private List<Comment> comments;
	private TextView textView;
	SharedPreferences mPrefs;
	String user;
	
	public MessageListViewAdapter (Context context, int textViewResourceId, List<Comment> com){
		super(context, textViewResourceId, com);
		comments = com;
		mPrefs = context.getSharedPreferences("myPrefs", context.MODE_PRIVATE);
		user = mPrefs.getString("user", "none");
	}
	
	
	public int getCount(){
		return comments.size();
	}
	
	public Comment getItem(int index){
		return comments.get(index);
	}
	
	
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		if (row == null) {
			LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.bubble_effect, parent, false);
		}
		
		wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
		Comment coment = getItem(position);
		textView = (TextView) row.findViewById(R.id.comment);
		textView.setText(coment.getComment());
		
		//Change to the users name == then this color or else that color
		textView.setBackgroundResource(!coment.getUser().toString().equals(user) ? R.drawable.bubble_yellow : R.drawable.bubble_green);
		wrapper.setGravity(!coment.getUser().toString().equals(user) ? Gravity.LEFT : Gravity.RIGHT);
		return row;
	}
	
	public Bitmap decodeToBitmap(byte[] decodedByte) {
		return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
	}
 
	
	
}
